package com.ma4567.remotecontroller.PermissionRequest;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ma4567.remotecontroller.RemoteControllerApplication;
import com.ma4567.remotecontroller.UserManager.UserManager;
import com.ma4567.remotecontroller.UserManager.UserPermission;
import com.ma4567.remotecontroller.ffmpegManager.ffmpegController;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

class SinglePermissionResponse {
    public int Status;//0 正常 1 cookie错误 2 permissionName错误
    public String PermissionName;
    public boolean Permission;

    public SinglePermissionResponse(int status, String permissionName, boolean permission) {
        this.Status = status;
        this.PermissionName = permissionName;
        this.Permission = permission;
    }
}

@CrossOrigin
@RestController
public class SinglePermission {
    //return user function list
    @RequestMapping(value = "/api/singlePermission", method = RequestMethod.GET)
    public String GetAllPermission(HttpServletRequest request,
                                   @RequestParam("cookie") String Cookie,
                                   @RequestParam("permissionName") String PermissionName){
        ffmpegController.StopStream();
        if(!UserPermission.IsPermissionExist(PermissionName)){
            return JSONObject.toJSONString(new SinglePermissionResponse(2, PermissionName, false), SerializerFeature.PrettyFormat);
        }

        if(!RemoteControllerApplication.userManager.JudgeCookie(Cookie)){
            return JSONObject.toJSONString(new SinglePermissionResponse(1, PermissionName, false), SerializerFeature.PrettyFormat);
        }
        else {
            return JSONObject.toJSONString(new SinglePermissionResponse(0, PermissionName, RemoteControllerApplication.userManager.GetUserSingleItemPermission(Cookie, PermissionName)), SerializerFeature.PrettyFormat);
        }
    }
}